package com.dream.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.hibernate.Hibernate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dream.common.Common;
import com.dream.common.DateTools;
import com.dream.common.JsonString;
import com.dream.common.SuperDao;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_ClassPlant;
import com.dream.model.Sys_ClassPlantMaster;
import com.dream.model.Sys_Lable;
import com.dream.model.Sys_LearnPlant;
import com.dream.model.Sys_LearnPlant_Class;
import com.dream.model.Sys_LearnPlant_Lable;
import com.dream.model.Sys_TestPaper;
import com.dream.model.Sys_User_Class;
import com.dream.model.Sys_User_TestPaper;
import com.dream.system.service.ISys_LearnPlant;

@Component("sys_LearnPlantServiceImpl")
@SuppressWarnings("all")
public class ISys_LearnPlantServiceImpl extends SuperDao<Object> implements ISys_LearnPlant {

	/**
	 * 增加一次备课
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Sys_LearnPlant addSys_LearnPlant(Sys_LearnPlant model, String arrclass, String arrlable) {
		// TODO Auto-generated method stub
		// 加入关联的班级
		if (null != arrclass && arrclass.length() > 2) {
			JSONArray jsonarr = JSONArray.fromObject(arrclass);
			List<Sys_Class> sclist = (List<Sys_Class>) JSONArray.toCollection(jsonarr, Sys_Class.class);
			if (sclist.size() > 0) {
				List<Sys_LearnPlant_Class> slclist = new ArrayList<Sys_LearnPlant_Class>();
				for (int i = 0; i < sclist.size(); i++) {
					Sys_LearnPlant_Class slc = new Sys_LearnPlant_Class();
					slc.setCreateTime(Common.getTime1());
					slc.setShowTime(Common.getTime());
					slc.setSys_Class(sclist.get(i));
					slc.setSys_LearnPlant(model);
					if ("1".equals(model.getPltype())) {
						slc.setState("0");
					} else if ("2".equals(model.getPltype())) {
						slc.setState("0");
					} else if ("3".equals(model.getPltype())) {
						slc.setState("0");
					} else if ("4".equals(model.getPltype())) {
						slc.setState("1");
					} else if ("5".equals(model.getPltype())) {
						slc.setState("1");
					}
					slclist.add(slc);
				}
				model.setListslpc(slclist);
			}
		}
		// 加入关联的标签
		if (null != arrlable && arrlable.length() > 2) {
			JSONArray jsonarr = JSONArray.fromObject(arrlable);
			List<Sys_Lable> sclist = (List<Sys_Lable>) JSONArray.toCollection(jsonarr, Sys_Lable.class);
			if (sclist.size() > 0) {
				List<Sys_LearnPlant_Lable> slclist = new ArrayList<Sys_LearnPlant_Lable>();
				for (int i = 0; i < sclist.size(); i++) {
					Sys_LearnPlant_Lable slc = new Sys_LearnPlant_Lable();
					slc.setCreateTime(Common.getTime1());
					slc.setShowTime(Common.getTime());
					slc.setSys_Lable(sclist.get(i));
					slc.setSys_LearnPlant(model);
					slclist.add(slc);
				}
				model.setListlpl(slclist);
			}
		}
		// 查询自己上级的名称
		String uuid = UUID.randomUUID().toString();
		model.setLpid(uuid);

		if ("1".equals(model.getPltype())) {
			model.setId1(uuid);
		} else if ("2".equals(model.getPltype())) {
			model.setId1(model.getPlpid());
			model.setId2(uuid);
		} else if ("3".equals(model.getPltype())) {
			model.setId1(model.getId1());
			model.setId2(model.getId2());
			model.setId3(uuid);
		} else if ("4".equals(model.getPltype())) {
			model.setId1(model.getId1());
			model.setId2(model.getId2());
			model.setId3(model.getId3());
			model.setId4(uuid);
		} else if ("5".equals(model.getPltype())) {
			model.setId1(model.getId1());
			model.setId2(model.getId2());
			model.setId3(model.getId3());
			model.setId4(model.getId3());
			model.setId5(uuid);
		}
		model.setCreateTime(Common.getTime1());
		model.setShowTime(Common.getTime());
		this.addT(model);
		return model;
	}

	/**
	 * 查询所有备课
	 */
	@SuppressWarnings("all")
	@Override
	public Map getSys_LearnPlant(Sys_LearnPlant model, SysUser sysUser) {
		// TODO Auto-generated method stub
		// String hql="from Sys_LearnPlant t ";
		// String opt =
		// " t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid, t.sysUser.userName,t.sysUser.table_id,t.showTime ";
		if ("2".equals(sysUser.getUserType())) {
			return getMyallp(model, sysUser);
		} else {
			return getAllp(model, sysUser);
		}
	}

	// 查询我的备课目录树
	public Map getMyallp(Sys_LearnPlant model, SysUser sysUser) {
		String hsql = "from Sys_LearnPlant t where ( t.pltype <> 5 and t.sysUser.table_id = '" + sysUser.getTable_id() + "' ) or (t.ltype='5' and  t.pltype <> 5 )  ";
		if (model != null && null != model.getName() && !"".equals(model.getName())) {
			hsql += " and t.name like '%" + model.getName() + "%'";
		}
		@SuppressWarnings("unchecked")
		List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList(hsql, 0, 0, "");
		int count = 0; // this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	// 查询所有的备课
	public Map getAllp(Sys_LearnPlant model, SysUser sysUser) {

		String hsql = "from Sys_LearnPlant t where pltype <> 5 ";
		if (model != null && null != model.getName() && !"".equals(model.getName())) {
			hsql += " and t.name like '%" + model.getName() + "%'";
		}
		@SuppressWarnings("unchecked")
		List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList(hsql, 0, 0, "");
		int count = 0; // this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	/**
	 * 查询当前目录下一的所有标签
	 */
	@SuppressWarnings("all")
	@Override
	public Map getAllLableByPltype1(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		// String hsql ="from Sys_LearnPlant t where t.id1 = ?";
		Object[] object = new Object[] { model.getId1() };
		// List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>)
		// this.getList(hsql,object,0,0, "");

		// if(null!=list1 && list1.size()>0){
		// }
		String opt = " t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid,t.showTime ";
		String hql = " from Sys_Lable t left join t.listlp t1 where t1.sys_LearnPlant.id1 = ? ";
		List<Sys_Lable> sllist = this.getList("select DISTINCT  new com.dream.model.Sys_Lable (" + opt + ") " + hql, object, 0, 0, "");
		int count = 0; // this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", sllist);
		map.put("sum", count);

		return map;
	}

	/**
	 * 删除课
	 */
	@Override
	public int deletelearnPlant(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		String pltype = "";
		if (null != model && null != model.getPltype()) {
			pltype = model.getPltype();
		} else {
			return 1;
		}
		String dsql = "";
		if ("1".equals(pltype)) {
			dsql = "delete from Sys_LearnPlant t where t.id1 = ? ";
		} else if ("2".equals(pltype)) {
			dsql = "delete from Sys_LearnPlant t where t.id2 = ? ";
		} else if ("3".equals(pltype)) {
			dsql = "delete from Sys_LearnPlant t where t.id3 = ? ";
		} else if ("4".equals(pltype)) {
			dsql = "delete from Sys_LearnPlant t where t.id4 = ? ";
		} else if ("5".equals(pltype)) {
			dsql = "delete from Sys_LearnPlant t where t.id5 = ? ";
		}
		Object[] obj = new Object[] { model.getLpid() };
		this.deleteT(dsql, obj);
		return 1;
	}

	/**
	 * 查询这一级所有级联的标签
	 */
	@SuppressWarnings("all")
	@Override
	public Map getLearnPlantLable(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		Object[] object = new Object[] { model.getTable_id() };
		String opt = " t.table_id, t.name, t.lableImportance, t.lableValue, t.type, t.jiaocai, t.lableId, t.lablePid,t.showTime ";
		String hql = " from Sys_Lable t left join t.listlp t1 where t1.sys_LearnPlant.table_id = ? ";
		List<Sys_Lable> sllist = this.getList("select DISTINCT  new com.dream.model.Sys_Lable (" + opt + ") " + hql, object, 0, 0, "");
		int count = 0; // this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", sllist);
		map.put("sum", count);
		return map;
	}

	/**
	 * 查询本级有多少个班级
	 */
	@SuppressWarnings("all")
	@Override
	public Map getLearnPlantClass(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		Object[] object = new Object[] { model.getTable_id() };
		String opt = " t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName, t.showTime ";
		String hql = " from Sys_Class t left join t.listslpc t1 where t1.sys_LearnPlant.table_id = ? ";
		List<Sys_Lable> sllist = this.getList("select DISTINCT  new com.dream.model.Sys_Class ( " + opt + " ) " + hql, object, 0, 0, "");
		int count = 0; // this.getListCount("select count(*) " + hsql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", sllist);
		map.put("sum", count);
		return map;
	}

	/**
	 * 更新当前的学期表
	 */
	@SuppressWarnings("all")
	@Override
	public Sys_LearnPlant updateSys_LearnPlant(Sys_LearnPlant model, String arrclass, String arrlable) {
		// TODO Auto-generated method stub

		Sys_LearnPlant slp = (Sys_LearnPlant) this.getT(new Sys_LearnPlant(), model.getTable_id());
		if (null == slp) {
			return null;
		}

		slp.getListlpl().clear();
		slp.getListslpc().clear();

		if (null != arrclass && arrclass.length() > 2) {
			JSONArray jsonarr = JSONArray.fromObject(arrclass);
			List<Sys_Class> sclist = (List<Sys_Class>) JSONArray.toCollection(jsonarr, Sys_Class.class);
			if (sclist.size() > 0) {
				List<Sys_LearnPlant_Class> slclist = new ArrayList<Sys_LearnPlant_Class>();
				for (int i = 0; i < sclist.size(); i++) {
					Sys_LearnPlant_Class slc = new Sys_LearnPlant_Class();
					slc.setCreateTime(Common.getTime1());
					slc.setShowTime(Common.getTime());
					slc.setSys_Class(sclist.get(i));
					slc.setSys_LearnPlant(slp);
					if ("1".equals(model.getPltype())) {
						slc.setState("0");
					} else if ("2".equals(model.getPltype())) {
						slc.setState("0");
					} else if ("3".equals(model.getPltype())) {
						slc.setState("0");
					} else if ("4".equals(model.getPltype())) {
						slc.setState("1");
					} else if ("5".equals(model.getPltype())) {
						slc.setState("1");
					}
					slp.getListslpc().add(slc);
				}

			}
		}
		// 加入关联的标签
		if (null != arrlable && arrlable.length() > 2) {
			JSONArray jsonarr = JSONArray.fromObject(arrlable);
			List<Sys_Lable> sclist = (List<Sys_Lable>) JSONArray.toCollection(jsonarr, Sys_Lable.class);
			if (sclist.size() > 0) {
				List<Sys_LearnPlant_Lable> slclist = new ArrayList<Sys_LearnPlant_Lable>();
				for (int i = 0; i < sclist.size(); i++) {
					Sys_LearnPlant_Lable slc = new Sys_LearnPlant_Lable();
					slc.setCreateTime(Common.getTime1());
					slc.setShowTime(Common.getTime());
					slc.setSys_Lable(sclist.get(i));
					slc.setSys_LearnPlant(slp);
					slp.getListlpl().add(slc);
				}

			}
		}
		slp.setContent(model.getContent());
		slp.setJiaoju(model.getJiaoju());
		slp.setLearntime(model.getLearntime());
		slp.setKeshi(model.getKeshi());
		slp.setName(model.getName());
		slp.setAdress(model.getAdress());
		this.updateT(slp);
		return slp;
	}

	/**
	 * 查询自己下一级的所有的步骤，并返回他们的标签放在arrable中
	 */
	@SuppressWarnings("all")
	@Override
	public JSONArray getLearnPlantChildren(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		if (null == model || null == model.getLpid() || "".equals(model.getLpid())) {
			return null;
		}
		String hql = "from Sys_LearnPlant t where t.plpid = ? ";
		Object[] obj = new Object[] { model.getLpid() };
		List<Sys_LearnPlant> list = this.getList(hql, obj, 0, 0, "");
		// 查询他所有的下级有的标签
		JSONArray jsonArray = new JSONArray();
		if (null != list && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Sys_LearnPlant slp = list.get(i);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("buzhou1", slp.getName());
				jsonObject.put("buzhou2", slp.getLearntime());
				jsonObject.put("buzhou3", slp.getAdress());
				if (null == slp.getFujian()) {
					jsonObject.put("buzhou4", "");// 附件
				} else {
					jsonObject.put("buzhou4", slp.getFujian());// 附件
				}

				jsonObject.put("buzhou5", slp.getContent());
				jsonObject.put("buzhou6", slp.getJiaoju());
				
				if (null == slp.getFujian()) {
					jsonObject.put("buzhou7", "");
				} else {
					jsonObject.put("buzhou7", slp.getFujianType());
				}
			

				Map map = this.getLearnPlantLable(list.get(i));
				List<Sys_Lable> listsl = (List<Sys_Lable>) map.get("list");
				if (null != listsl && listsl.size() > 0) {
					JSONArray jsonArray2 = new JSONArray();
					for (int j = 0; j < listsl.size(); j++) {
						JSONObject joJsonObject = new JSONObject();
						joJsonObject.put("table_id", listsl.get(j).getTable_id());
						joJsonObject.put("name", listsl.get(j).getName());
						jsonArray2.add(joJsonObject);
					}
					jsonObject.put("arrlable", jsonArray2.toString());
				}
				jsonArray.add(jsonObject);
			}
		}
		return jsonArray;
	}

	/**
	 * 删除自己的下一级所有
	 */
	@Override
	public int deletelearnPlantChildren(Sys_LearnPlant map) {
		// TODO Auto-generated method stub
		String hql = "delete from Sys_LearnPlant t where t.plpid = ? ";
		Object[] object = new Object[] { map.getLpid() };
		this.deleteT(hql, object);
		return 1;
	}

	/**
	 * 查询当前这个节点下的所有环节
	 */
	@Override
	public Map getAlllearnPlantBy4(Sys_LearnPlant model, SysUser sysUser) {
		// TODO Auto-generated method stub

		if (!"2".equals(sysUser.getUserType())) {
			return getAll(model, sysUser);
		} else {
			return getTeacherPlant(model, sysUser);
		}

	}

	/**
	 * 查询老师自己备课和专家的备课
	 * 
	 * @param model
	 * @param sysUser
	 * @return
	 */
	public Map getTeacherPlant(Sys_LearnPlant model, SysUser sysUser) {
		String dsql = "";
		String opt = "t.share,t.table_id, t.name, t.content, t.adress,t.learntime, t.keshi, t.jiaoju, t.nextTime, t.pltype, t.ltype,t.pStartTime,t.pendTime, t.pStartShowTime, t.pendTimeShow, t.lpid, t.plpid, t.pName, t.id1, t.id2, t.id3, t.id4, t.id5,t1.table_id,t1.name,t.showTime,t.sysUser.userEngListName";

		if (null == model || null == model.getLpid() || "".equals(model.getLpid())) {
			String name = "";
			if (null != model.getName()) {
				name = model.getName();
			}
			// 这里把卷的名称和Id放在了string2和string1里面
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where (t.pltype ='4' and t.sysUser.table_id = '" + sysUser.getTable_id() + "' and t.name like '%" + name + "%') or ( t.ltype ='5' and t.pltype ='4' )";
			@SuppressWarnings("unchecked")
			List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList("select new com.dream.model.Sys_LearnPlant(" + opt + ")" + dsql, model.getStart(), model.getLimit(), "");
			int count = this.getListCount("select count(*) " + dsql, "");
			Map map = new HashMap();
			map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
			map.put("list", list1);
			map.put("sum", count);
			return map;
		}
		String pltype = model.getPltype();
		if ("1".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where (t.id1 = ? and t.pltype ='4' and t.sysUser.table_id ='" + sysUser.getTable_id() + "')  or (t.ltype ='5' and t.pltype ='4' and  t.id1 = ?)";
		} else if ("2".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where (t.id2 = ? and t.pltype ='4' and t.sysUser.table_id ='" + sysUser.getTable_id() + "')  or (t.ltype ='5' and t.pltype ='4' and  t.id2 = ?)";
		} else if ("3".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where (t.id3 = ? and t.pltype ='4' and t.sysUser.table_id ='" + sysUser.getTable_id() + "')  or (t.ltype ='5' and t.pltype ='4' and  t.id3 = ?)";
		} else if ("4".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where (t.id4 = ? and t.pltype ='4' and t.sysUser.table_id ='" + sysUser.getTable_id() + "')  or (t.ltype ='5' and t.pltype ='4' and  t.id4 = ?)";
		} else if ("5".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where (t.id5 = ? and t.pltype ='4' and t.sysUser.table_id ='" + sysUser.getTable_id() + "')  or (t.ltype ='5' and t.pltype ='4' and  t.id5 = ?)";
		}
		if (null != model.getName() && !"".equals(model.getName())) {
			dsql += " and t.name like '%" + model.getName() + "%'";
		}
		Object[] obj = new Object[] { model.getLpid(), model.getLpid() };
		@SuppressWarnings("unchecked")
		List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList("select new com.dream.model.Sys_LearnPlant(" + opt + ")" + dsql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + dsql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	// 查询所有的备课
	public Map getAll(Sys_LearnPlant model, SysUser sysUser) {
		String dsql = "";
		String opt = "t.share,t.table_id, t.name, t.content, t.adress,t.learntime, t.keshi, t.jiaoju, t.nextTime, t.pltype, t.ltype,t.pStartTime,t.pendTime, t.pStartShowTime, t.pendTimeShow, t.lpid, t.plpid, t.pName, t.id1, t.id2, t.id3, t.id4, t.id5,t1.table_id,t1.name,t.showTime,t.sysUser.userEngListName";

		if (null == model || null == model.getLpid() || "".equals(model.getLpid())) {
			String name = "";
			if (null != model.getName()) {
				name = model.getName();
			}
			// 这里把卷的名称和Id放在了string2和string1里面
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where t.pltype ='4' and t.name like '%" + name + "%' ";
			@SuppressWarnings("unchecked")
			List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList("select new com.dream.model.Sys_LearnPlant(" + opt + ")" + dsql, model.getStart(), model.getLimit(), "");
			int count = this.getListCount("select count(*) " + dsql, "");
			Map map = new HashMap();
			map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
			map.put("list", list1);
			map.put("sum", count);
			return map;
		}
		String pltype = model.getPltype();
		if ("1".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where t.id1 = ? and t.pltype ='4' ";
		} else if ("2".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where t.id2 = ? and t.pltype ='4' ";
		} else if ("3".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where t.id3 = ? and t.pltype ='4' ";
		} else if ("4".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where t.id4 = ? and t.pltype ='4' ";
		} else if ("5".equals(pltype)) {
			dsql = " from Sys_LearnPlant t left join t.sys_TestPaper t1 where t.id5 = ? and t.pltype ='4' ";
		}
		if (null != model.getName() && !"".equals(model.getName())) {
			dsql += " and t.name like '%" + model.getName() + "%'";
		}
		Object[] obj = new Object[] { model.getLpid() };
		@SuppressWarnings("unchecked")
		List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList("select new com.dream.model.Sys_LearnPlant(" + opt + ")" + dsql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + dsql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	/**
	 * 
	 * 创建课程和作业的关联
	 * 
	 */
	@Override
	public int updateLearnPlantHomeWork(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		if (null == model || null == model.getTable_id() || "".equals(model.getTable_id())) {

			return 0;
		}
		Sys_LearnPlant lp = (Sys_LearnPlant) this.getT(new Sys_LearnPlant(), model.getTable_id());
		lp.setSys_TestPaper(model.getSys_TestPaper());
		this.updateT(lp);
		return 1;
	}

	/***
	 * 
	 * 
	 * 得到当前登录人，需要上的下一堂课
	 */
	@Override
	public Map getClassBegin(SysUser sysUser) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		try {
			Object[] obj = new Object[] { sysUser.getTable_id(), Common.getTime1() };
			String hql = "from Sys_ClassPlantMaster t where t.sysUser.table_id = ? and t.classTime > ? ";
			List<Sys_ClassPlantMaster> list = this.getList(hql, obj, 0, 1, "order by t.classTime asc ");
			if (list == null || list.size() <= 0) {
				return null;
			} else {
				Sys_ClassPlantMaster scp = list.get(0);
				String classId = scp.getSys_Class().getName(); // 班
				String plantId = scp.getSys_LearnPlant().getName(); // 得到计划
				if (null != scp.getSys_LearnPlant().getListlpl()) {
					List<Sys_LearnPlant_Lable> listsbl = scp.getSys_LearnPlant().getListlpl();
					List<Sys_Lable> listsl = new ArrayList<Sys_Lable>();
					for (int i = 0; i < listsbl.size(); i++) {
						Sys_Lable sl = listsbl.get(i).getSys_Lable();
						Sys_Lable sl1 = new Sys_Lable();
						System.err.println(sl.getTable_id());
						sl1.setName(sl.getName());
						sl1.setLableId(sl.getLableId());
						sl1.setLableValue(sl.getLableValue());
						sl1.setTable_id(sl.getTable_id());
						//BeanUtils.copyProperties(sl, sl1, JsonString.sys_lable);
						listsl.add(sl1);
					}
					JsonConfig jc = new JsonConfig();
					jc.setExcludes(JsonString.sys_lable);
					map.put("arrLable", JSONArray.fromObject(listsl, jc).toString());
				}
				JsonConfig jc = new JsonConfig();
				jc.setExcludes(JsonString.sys_classPlantMaster);
				map.put("sys_ClassPlantMaster", JSONObject.fromObject(scp, jc).toString());
				if (null != classId) {
					JsonConfig jc1 = new JsonConfig();
					jc1.setExcludes(JsonString.sys_class);
					Sys_Class sc = new Sys_Class(); 
					BeanUtils.copyProperties(scp.getSys_Class(), sc, JsonString.sys_class);
					if (scp.getSys_Class().getListsuc() != null) {
						for (int i = 0; i < scp.getSys_Class().getListsuc().size(); i++) {
							Sys_User_Class suc = scp.getSys_Class().getListsuc().get(i);
							if ("1".equals(suc.getState())) {
								sc.setString1(suc.getYear());
							}
						}
					}

					// Hibernate.initialize(sc);
					// System.out.println(JSONObject.fromObject(sc,
					// jc1).toString());
					map.put("sys_Class", sc);
				}
				if (null != plantId) {
					JsonConfig jc1 = new JsonConfig();
					jc1.setExcludes(JsonString.sys_LearnPlant);
					Sys_LearnPlant slp = new Sys_LearnPlant();
					BeanUtils.copyProperties(scp.getSys_LearnPlant(), slp, JsonString.sys_LearnPlant);
					map.put("sys_LearnPlant", slp);
				}
				// 得到当前计划的所有下一级
				JSONArray children = this.getLearnPlantChildren(scp.getSys_LearnPlant());
				map.put("children", children);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public int updatePlantHomeWork(Sys_LearnPlant sys_LearnPlant, Sys_Class sc) {
		// TODO Auto-generated method stub
		Sys_LearnPlant scp = (Sys_LearnPlant) this.getT(new Sys_LearnPlant(), sys_LearnPlant.getTable_id());
		if (null == scp) {
			return 0;
		}
		Sys_Class sc1 = (Sys_Class) this.getT(new Sys_Class(), sc.getTable_id());
		// 查询班里面的所有学生当前这一届，状态为1，是学生类型，在这个班的
		String hql = "select t from SysUser t left join t.listsuc t1 where t1.sys_Class.table_id = ? and t1.state='1' and t1.year = ? and t.userType ='1'";
		Object[] obj = new Object[] { sc.getTable_id(), DateTools.getCurrentYear() };
		List<SysUser> sulist = this.getList(hql, obj, 0, 0, "");
		if (null != sulist) {
			String cql = "select count(*) from Sys_User_TestPaper t where t.sysUser.table_id = ?  and t.sys_TestPaper.table_id = ? ";
			Object[] obj1 = null;
			for (int i = 0; i < sulist.size(); i++) {
				SysUser su = sulist.get(i);
				obj1 = new Object[] { su.getTable_id(), scp.getSys_TestPaper().getTable_id() };
				int count = this.getListCount(cql, obj1, "");
				if (count > 0) {
					continue;
				}
				Sys_User_TestPaper sut = new Sys_User_TestPaper();
				sut.setSysUser(su);
				sut.setSys_TestPaper(scp.getSys_TestPaper());
				this.addT(sut);
			}
		}
		return 1;
	}

	/**
	 * 
	 * 完成上课，把模板中的关联放到正式的课程中去
	 * 
	 * @param sys_LearnPlant
	 * @param sc
	 * @param sysUser
	 * @return
	 */
	@Override
	public int updatePlantMasterClose(Sys_LearnPlant sys_LearnPlant, Sys_Class sc, SysUser sysUser) {
		// TODO Auto-generated method stub
		Sys_LearnPlant slp = (Sys_LearnPlant) this.getT(new Sys_LearnPlant(), sys_LearnPlant.getTable_id());
		if (null == slp) {
			return 0;
		}
		// 表示一个计划，只能在一个班上一次，只能一年中上一次
		String hql = "select count(*) from Sys_ClassPlant t where t.sys_Class.table_id = ? and t.sys_LearnPlant.table_id = ? and t.year = ? ";
		Object[] obj1 = new Object[] { sc.getTable_id(), sys_LearnPlant.getTable_id(), DateTools.getCurrentYear() };

		int count = this.getListCount(hql, obj1, "");
		if (count > 0) {
			return 2;
		}
		Object[] obj = new Object[] { sc.getTable_id(), sys_LearnPlant.getTable_id() };
		Sys_ClassPlant scp = new Sys_ClassPlant();
		scp.setSys_Class(sc);
		scp.setSysUser(sysUser);
		scp.setSys_LearnPlant(slp);
		scp.setCreateTime(Common.getTime1());
		scp.setShowTime(Common.getTime());
		scp.setTimeStart(sys_LearnPlant.getpStartTime());
		scp.setTimeEnd(sys_LearnPlant.getPendTime());
		scp.setTimeStartShow(sys_LearnPlant.getpStartShowTime());
		scp.setTimeEndShow(sys_LearnPlant.getPendTimeShow());
		scp.setState("1");
		scp.setYear(DateTools.getCurrentYear());
		this.addT(scp);
		return 1;
	}

	/**
	 * 
	 * 查询这节课在哪个课表上，在课排 表的关联当
	 * 
	 * 
	 */
	@Override
	public Sys_ClassPlantMaster getLearnPlant_masterPlant(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		String hql = "from Sys_ClassPlantMaster t where t.sys_LearnPlant.table_id = ? ";

		Object[] obj = new Object[] { model.getTable_id() };

		Sys_ClassPlantMaster scp = (Sys_ClassPlantMaster) this.getListOne(hql, obj, "");

		return scp;

	}

	/**
	 * 
	 * 查询一节课关联的作业
	 * 
	 */
	@Override
	public Sys_TestPaper getLearnPlant_HomeWork(Sys_LearnPlant model) {
		String hql = "from Sys_LearnPlant t where t.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		Sys_LearnPlant scp = (Sys_LearnPlant) this.getListOne(hql, obj, "");
		Sys_TestPaper st = new Sys_TestPaper();
		BeanUtils.copyProperties(scp.getSys_TestPaper(), st);
		return st;
	}

	@Override
	public JSONArray getBeikeJuan(Sys_LearnPlant model) {
		// TODO Auto-generated method stub

		String sql = "from Sys_TestPaper t where t.sys_LearnPlant.table_id = ? ";

		Object[] obj = new Object[] { model.getTable_id() };

		List<Sys_TestPaper> slist = this.getList(sql, obj, 0, 100, "");
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.test_Paper);
		JSONArray js = JSONArray.fromObject(slist, jc);

		return js;
	}

	/**
	 * 
	 * 查询一级备课
	 */
	@Override
	public Sys_LearnPlant getSys_LearnPlantById(Sys_LearnPlant model) {
		// TODO Auto-generated method stub
		Sys_LearnPlant sl = (Sys_LearnPlant) this.getT(new Sys_LearnPlant(), model.getTable_id());
		return sl;

	}

}
